﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using BreakOutBattles.IO;
using System.Timers;
#endregion

namespace BreakOutBattles.Game
{
    public class MatchReportViewer
    {
        #region Fields
        private const double WAIT_TO_MAIN_MENU = 5000;
        private const double SCALE_DELAY = 20;
        private const string TIME_FORMAT = @"mm\:ss";

        private IOManager ioManager;

        private Texture2D background;
        private SpriteFont winnerNameFont;
        private SpriteFont defaultFont;

        private Timer toMainMenuTimer;
        private Timer scaleTimer;
        private float minWinnerScale = 0.9f;
        private float maxWinnerScale = 1.1f;
        private float scaleStep = 0.03f;
        private float currentScale = 1.0f;
        private float scaleDirection = 1;
        
        private string winnerHeading = "WINS!";
        private string matchTimeHeading = "Match Time: ";
        private string reportMessage = "Finishing Match....";
        private string matchTimeStr = string.Empty;

        private Vector2 winnerNamePos;
        private Vector2 winnerHeadingPos;
        private Vector2 matcTimehHeadingPos;
        private Vector2 matchTimePos;
        private Vector2 reportMessagePos;
        private Vector2 scaleOrigin;

        private bool isReady = false;
        #endregion

        public MatchReportViewer()
        {
            ioManager = IOManager.Instance;
            LoadContent();
            Initialize();
        }

        #region Initialization
        /// <summary>
        /// Loads resources used by this object.
        /// </summary>
        private void LoadContent()
        {
            background = ioManager.LoadTexture(ContentFolders.MISC, "matchReportBckg");
            winnerNameFont = ioManager.LoadFont("matchWinnerFont");
            defaultFont = ioManager.LoadFont("matchReportDefaultFont");
        }

        /// <summary>
        /// Initializes all default components of this class.
        /// </summary>
        private void Initialize()
        {
            winnerNamePos = new Vector2(640, 150);

            matcTimehHeadingPos = new Vector2(420, 550);
            winnerHeadingPos = new Vector2(640 - ToolBox.StringWidth(winnerHeading, winnerNameFont) / 2, 230);

            matchTimePos = new Vector2(420 + ToolBox.StringWidth(matchTimeHeading, defaultFont), 550);
            reportMessagePos = new Vector2(640 - ToolBox.StringWidth(reportMessage, defaultFont) / 2, 600);

            scaleTimer = new Timer(SCALE_DELAY);
            scaleTimer.Elapsed += ScaleWinnerName;

            toMainMenuTimer = new Timer(WAIT_TO_MAIN_MENU);
            toMainMenuTimer.Elapsed += GoToMainMenu;
        }
        #endregion

        /// <summary>
        /// Initializes MatchReport objects that needs to be initiliazed or updated at different game states and
        /// not during the creation of a MatchReportViewer instance.
        /// </summary>
        private void InitializeManualObjects()
        {
            matchTimeStr = MatchReport.MatchTime.ToString(TIME_FORMAT);

            float stringCenterX = ToolBox.StringWidth(MatchReport.Winner.Name, winnerNameFont) - ToolBox.StringWidth(MatchReport.Winner.Name, winnerNameFont) / 2;
            float stringCenterY = ToolBox.StringHeight(MatchReport.Winner.Name, winnerNameFont) - ToolBox.StringHeight(MatchReport.Winner.Name, winnerNameFont) / 2;
            scaleOrigin = new Vector2(stringCenterX, stringCenterY);
                       
            scaleTimer.Start();
            toMainMenuTimer.Start();

            isReady = true;
        }

        /// <summary>
        /// Resets all componenets to its initial state.
        /// </summary>
        public void Reset()
        {
            scaleTimer.Stop();
            toMainMenuTimer.Stop();
            scaleDirection = 1;
            currentScale = 1;
            isReady = false;
        }

        /// <summary>
        /// Terminates any running operations in the MatchReportViewer and changes 
        /// the current game state to notify the game to go to the main menu.
        /// </summary>
        private void GoToMainMenu(object sender, ElapsedEventArgs args)
        {
            Reset();
            GameState.CurrentState = GameStates.MENU;
        }

        /// <summary>
        /// Scales the winner name up or down with some units, one time at each call.
        /// </summary>
        private void ScaleWinnerName(object sender, ElapsedEventArgs args)
        {
            if (currentScale > maxWinnerScale)
            {
                scaleDirection = -1;
            }
            else if (currentScale < minWinnerScale)
            {
                scaleDirection = 1;
            }

            currentScale += scaleDirection * scaleStep;
        }

        /// <summary>
        /// Updates the MatchReportViewer.
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            if (!isReady)
            {
                InitializeManualObjects();
            }
        }

        /// <summary>
        /// Renders the contents of the MatchReportViewer.
        /// </summary>
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(background, Vector2.Zero, Color.White);
            spriteBatch.DrawString(winnerNameFont, MatchReport.Winner.Name, winnerNamePos, Color.Red, 0, scaleOrigin, currentScale, SpriteEffects.None, 0);
            spriteBatch.DrawString(winnerNameFont, winnerHeading, winnerHeadingPos, Color.White);
            spriteBatch.DrawString(defaultFont, matchTimeHeading, matcTimehHeadingPos, Color.White);
            spriteBatch.DrawString(defaultFont, matchTimeStr, matchTimePos, Color.White);
            spriteBatch.DrawString(defaultFont, reportMessage, reportMessagePos, Color.White);
        }
    }
}
